package com.woniuxy.controller;


import com.woniuxy.entity.Activity;
import com.woniuxy.entity.ActivityOrder;
import com.woniuxy.entity.User;
import com.woniuxy.service.ActitvityService;
import com.woniuxy.util.Page;
import com.woniuxy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class ActitvityController {

    @Autowired
    private ActitvityService actitvityService;

    @GetMapping("activity/campList")
    private Result selectActivityExampleList(String city) throws Exception {

        List<Map<String, Object>> list1 = actitvityService.selectActivityType();
        List<Map<String, Object>> list2 = actitvityService.selectCampByCity(city);
        ArrayList<Map<String, Object>> list3 = actitvityService.selectActivtyList(city);
        ArrayList<List<Map<String, Object>>> list = new ArrayList<>();
        list.add(list2);
        list.add(list1);
        list.add(list3);

        return new Result("","",null,list);
    }

    @GetMapping("activity/selectActivity")
    public Result selectActivity(Integer actId) throws Exception{
        List<Map<String ,Object>> list = actitvityService.selectActivityById(actId);

        return new Result("","",list.get(0),null);
    }

    @PostMapping("activity/addActiveOrder")
    public Result addActiveOrder(ActivityOrder order) throws  Exception{
        Integer id = actitvityService.saveActiveOrder(order);
       return new Result("","",id,null);
    }

    @GetMapping("activity/selecrAllOrder")
    public Result selecrAllOrderByStatus(Page page,Integer index ,Integer userId) throws Exception{

        System.out.println(page);
        int count =page.getCount();
        if(count == -1){
            count = actitvityService.selectCountByStatusAndUserId(index,userId);
        }
        int pageCount = count % page.getNumber() == 0 ? count / page.getNumber() : count /page.getNumber() + 1;
        page.setPageCount(pageCount);
        page.setCount(count);

        List<Map<String, Object>> list = actitvityService.selectByStatusAndUserId(page,index,userId);
        return new Result("","",page,list);
    }

    @DeleteMapping("activity/deleteOrderById/{id}")
    public Result deleteOrderById(@PathVariable("id") Integer id) throws Exception{
        System.out.println(id+"------------------");
        actitvityService.deleteOrderById(id);
        return new Result("","1",null,null);
    }
    @PostMapping("buyActive")
    public Result buyActive(User user,Integer number,Integer id,Integer actId) throws Exception{

        System.out.println(user+"-------------------");
        actitvityService.updateActiveById(user,number,id,actId);

        return new Result("","1",null,null);
    }
}
